<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/18 22:33
 */

namespace module\user\logic\admin;

use module\user\model\User;
use mof\annotation\Inject;
use mof\Logic;
use mof\Model;

class UserLogic extends Logic
{
    #[Inject(User::class)]
    protected $model;

    public function update($id, $params): Model
    {
        //空密码表示不修改密码字段
        foreach (['vip_expire', 'password'] as $key) {
            if (isset($params[$key]) && trim($params[$key]) === '') {
                unset($params[$key]);
            }
        }
        if ($params['vip'] === 0) {
            $params['vip_expire'] = null;
        }
        return parent::update($id, $params);
    }

    public function delete($id): bool
    {
        $model = $this->read($id);
        if ($result = $model->delete()) {
            //用户账号删除事件
            event('UserDelete', $model);
        }
        return $result;
    }
}